"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具新建目录之-p path参数验证
Create At   : 2023/4/13
Owner       : opentestcase019
Description :
    1.新建目录，-p path参数指定正常路径
    2.新建目录，-p path指定不存在的路径
    3.新建目录，-p path指定不以+开头的路径
    4.新建目录，-p path指定非共享存储的路径
    5.新建目录，-p path指定包含特殊字符的路径
    6.在集群停止的情况下执行
    7.在dss关闭的情况下执行
Expect      :
    1.创建成功
    2.合理报错
    3.合理报错
    4.合理报错
    5.合理报错
    6.合理报错
    7.合理报错
History     :
"""
import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.dir_name_1 = "dir_dsscmd_0018_1"
        self.dir_name_2 = "dir_dsscmd_0018_2"
        self.logger.info("-----获取卷组名-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.", execute_msg, "获取卷组名失败")
        self.vgname = execute_msg.splitlines()[1].split()[0].strip()
        self.logger.info(self.vgname)
        self.path = os.path.join(self.vgname, self.dir_name_1)

    def test_sharestorage_tool(self):
        step1 = "-----step1:新建目录，-p path参数指定正常路径;expect:创建成功-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_1}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Succeed to make dir, path is +{self.vgname}, "
                      f"dir name is {self.dir_name_1}.",
                      execute_msg, "执行失败" + step1)

        step2 = "-----step2:新建目录，-p path指定不存在的路径;expect:合理报错-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +test -d {self.dir_name_2}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Failed to find volume group test, "
                      f"please check vg config file.",
                      execute_msg, "执行失败" + step2)

        step3 = "-----step3:新建目录，-p path指定不以+开头的路径;expect:合理报错-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p {self.vgname} -d {self.dir_name_2}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"cmd mkdir error:2193 Path {self.vgname} decode "
                      f"error , path should start with +.",
                      execute_msg, "执行失败" + step3)

        step4 = "-----step4:新建目录，-p path指定非共享存储的路径;expect:合理报错-----"
        self.logger.info(step4)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p {os.path.dirname(macro.DB_INSTANCE_PATH)} " \
            f"-d {self.dir_name_2}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"cmd mkdir error:2193 Path "
                      f"{os.path.dirname(macro.DB_INSTANCE_PATH)} "
                      f"decode error , path should start with +.",
                      execute_msg, "执行失败" + step4)

        step5 = "-----step5:新建目录，-p path指定包含特殊字符的路径;expect:合理报错-----"
        self.logger.info(step5)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname}@ -d {self.dir_name_2};" \
            f"dsscmd mkdir -p +{self.vgname}$ -d {self.dir_name_2}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertEquals(2, execute_msg.count(
            "path should be [0~9,a~z,A~Z,-,_,/,.]."), "执行失败" + step5)

        step6 = "-----step6:在集群停止的情况下执行;expect:合理报错-----"
        self.logger.info(step6)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_2}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step6)
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('cluster_state   : Normal', status, "执行失败" + step6)

        step7 = "-----step7:在dss关闭的情况下执行;expect:合理报错-----"
        self.logger.info(step7)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd stopdss;" \
            f"sleep 2;" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_2}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step7)

    def tearDown(self):
        self.logger.info("-----恢复环境-----")
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        if "cluster_state   : Normal" not in status:
            start_msg = self.primary_sh.exec_cm_ctl(mode='start')
            self.logger.info(start_msg)
        time.sleep(30)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd rmdir -p +{self.path}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('cluster_state   : Normal', status, '执行失败')
        self.assertIn(f"Succeed to rm dir, path is +{self.path}",
                      execute_msg, "恢复环境失败")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
